// ----------------------------------
// RSDK Project: Sonic Essence
// Script Description: Soul Octopoid Body
// Script Author: Tails LP
// ----------------------------------

//-------Aliases-------//

//States

private alias 0 : OCTOPOID_INT
private alias 1 : OCTOPOID_AWAITPLAYER
private alias 2 : OCTOPOID_RISEUP
private alias 3 : OCTOPOID_WAIT
private alias 4 : OCTOPOID_ARMAPPEAR
private alias 5 : OCTOPOID_ARMCONTROL
private alias 6 : OCTOPOID_ARMIDLE
private alias 7 : OCTOPOID_ROCKETLAUNCH
private alias 8 : OCTOPOID_MOVEX
private alias 9 : OCTOPOID_ROCKETCOMEBACK
private alias 10 : TEASE_END


private alias object.value38 : player.hitboxLeft
private alias object.value39 : player.hitboxRight
private alias object.value40 : player.hitboxTop
private alias object.value41 : player.hitboxBottom

private alias object.value0 : boss.timer
private alias object.value1 : boss.launchertimer
private alias object.value2 : boss.launcherframe
private alias object.value3 : boss.shaketimer
private alias object.value4 : boss.screenShake
private alias object.value5 : area.leftBorder
private alias object.value6 : area.rightBorder

//Arm values

private alias object.value4 : check.overlap0
private alias object.value5 : check.overlap1
private alias object.value6 : check.overlap2
private alias object.value7 : check.overlap3

//Rocket values

private alias object.value2 : rocket.offsetTime

//-------Function declarations-------//

//-------Static Values-------//

public value rise.topYPos = 0;
public value rise.bottomYPos = 0;
public value body.hit = 0;

//-------Tables-------//


//-------Functions-------//

reserve function Launcher_Animation

function Launcher_Animation
	boss.launchertimer ++
	
	switch boss.launchertimer
	case 0
		boss.launcherframe = 2
		break
	case 8
		boss.launcherframe = 3
		break
	case 16
		boss.launcherframe = 4
		break
	case 24
		temp0 = object.xpos
		temp1 = object.ypos

		PlaySfx(SfxName[Octo Missile Launch], false)

		temp1 += 0xB0000
		CreateTempObject(TypeName[Soul Octopoid Rocket], 0, temp0, temp1)
		object[tempObjectPos].frame = 3
		rocket[tempObjectPos].offsetTime = 150
		
		break
	case 44
		temp0 = object.xpos
		temp1 = object.ypos

		PlaySfx(SfxName[Octo Missile Launch], false)

		temp0 += 0x300000
		temp1 += 0xB0000
		CreateTempObject(TypeName[Soul Octopoid Rocket], 0, temp0, temp1)
		object[tempObjectPos].frame = 3
		rocket[tempObjectPos].offsetTime = 200
		
		PlaySfx(SfxName[Octo Missile Launch], false)

		temp0 = object.xpos
		temp0 -= 0x300000
		CreateTempObject(TypeName[Soul Octopoid Rocket], 0, temp0, temp1)
		object[tempObjectPos].frame = 3
		rocket[tempObjectPos].offsetTime = 200
		
		break
	case 64
		temp0 = object.xpos
		temp1 = object.ypos

		PlaySfx(SfxName[Octo Missile Launch], false)

		temp0 += 0x4A0000
		temp1 += 0xB0000
		CreateTempObject(TypeName[Soul Octopoid Rocket], 1, temp0, temp1)
		object[tempObjectPos].frame = 3
		rocket[tempObjectPos].offsetTime = 250
		
		PlaySfx(SfxName[Octo Missile Launch], false)

		temp0 = object.xpos
		temp0 -= 0x4A0000
		CreateTempObject(TypeName[Soul Octopoid Rocket], 1, temp0, temp1)
		object[tempObjectPos].frame = 3
		rocket[tempObjectPos].offsetTime = 250
		
		break
	case 120
		boss.launcherframe = 3
		break
	case 128
		boss.launchertimer = 0
		boss.launcherframe = 2
		break
	endswitch
end function


event ObjectMain
	
	switch object.state
	case OCTOPOID_INT
		object.direction = FLIP_X // this this to mirrow the launcher sprites
		object.iypos += 256
		boss.screenShake = 64

		object.state ++
		break
		
	case OCTOPOID_AWAITPLAYER
		if object[0].xpos > object.xpos

			temp0 = object.ixpos
			temp0 -= screen.xcenter
			stage.newXBoundary1 = temp0
			area.leftBorder = temp0	
			area.leftBorder += 16
			area.leftBorder *= 65536

			temp0 = object.ixpos
			temp0 += screen.xcenter
			stage.newXBoundary2 = temp0
			area.rightBorder = temp0	
			area.rightBorder -= 16
			area.rightBorder *= 65536
			
			ResetObjectEntity(26, TypeName[Music Event], 0, 0, 0)
			object[26].priority = PRIORITY_ACTIVE
			
			object.state ++
		endif
		break
		
	case OCTOPOID_RISEUP
		foreach (TypeName[Boss Title Card], arrayPos0, ALL_ENTITIES)
			object[arrayPos0].priority = PRIORITY_ACTIVE_PAUSED
		next

		boss.timer ++
		if boss.timer >= 20
			if object.ypos > rise.topYPos
				PlaySfx(SfxName[Octo Rising], false)
				object.ypos -= 0x10000
			else
				object.ypos = rise.topYPos
				object.state ++
				StopSfx(SfxName[Octo Rising], false)
				boss.timer = 0
			endif
		endif
		
		boss.shaketimer++
		
		if boss.shaketimer== 30
			boss.screenShake = 0xFFFF
		end if


		temp2 = boss.screenShake
		temp2 %= 40
		if temp2 == 8
			
		end if
		break
		
	case OCTOPOID_WAIT
		boss.timer++
		if boss.timer > 300
			boss.timer = 0
			object.state ++
		endif
			
		break
		
	case OCTOPOID_ARMAPPEAR
		boss.timer++
		if boss.timer < 40
			PlaySfx(SfxName[Octo Rising], false)
			object[+1].ypos -= 0x40000
			object[+2].ypos -= 0x40000
			object[+3].ypos -= 0x40000
			object[+4].ypos -= 0x40000
		else
			StopSfx(SfxName[Octo Rising], false)
			if boss.timer < 86
				object[+1].ypos += 0x20000
				object[+2].ypos += 0x20000
				object[+3].ypos += 0x20000
				object[+4].ypos += 0x20000
			else
				boss.timer = 0
				object.state ++
				
				object[+1].state ++
				object[+2].state ++
				object[+3].state ++
				object[+4].state ++
			endif
		endif
		break
	case OCTOPOID_ARMCONTROL
		
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.ixpos > object[currentPlayer].ixpos
				object.xpos -= 0x10000
			else
				if object.ixpos < object[currentPlayer].ixpos
					object.xpos += 0x10000
				//else
				//	object.ixpos = object[currentPlayer].ixpos
				endif
			endif
		next
		foreach (TypeName[Soul Octopoid Arms], arrayPos1, ALL_ENTITIES)
			checkLower(object[arrayPos1].xpos, area.leftBorder)
			temp0 = checkResult
			checkGreater(object[arrayPos1].xpos, area.rightBorder)
			temp1 = checkResult
			
			if temp0 == 1
				object[arrayPos1].direction = 0
			endif		
			
			if temp1 == 1
				object[arrayPos1].direction = 1
			endif
			
		next
		boss.timer++
		if boss.timer > 300
			boss.timer = 0
			object.state ++
			if object[+1].state < 3
				object[+1].state ++
			endif
			if object[+2].state < 3
				object[+2].state ++
			endif
			if object[+3].state < 3
				object[+3].state ++
			endif
			if object[+4].state < 3
				object[+4].state ++
			endif
		endif
		break
	case OCTOPOID_ARMIDLE
		boss.timer++
		if boss.timer > 290
			boss.timer = 0
			object.state --	
			if object[+1].state < 3
				object[+1].state ++
			endif
			if object[+2].state < 3
				object[+2].state ++
			endif
			if object[+3].state < 3
				object[+3].state ++
			endif
			if object[+4].state < 3
				object[+4].state ++
			endif
		endif
		break
	case OCTOPOID_ROCKETLAUNCH
		boss.timer++
		if boss.timer > 128
			boss.timer = 0
			object.state = OCTOPOID_MOVEX
		endif
		callFunction(Launcher_Animation)
		break
		
	case OCTOPOID_MOVEX
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.ixpos > object[currentPlayer].ixpos
				object.xpos -= 0x10000
			else
				if object.ixpos < object[currentPlayer].ixpos
					object.xpos += 0x10000
				//else
				//	object.ixpos = object[currentPlayer].ixpos
				endif
			endif
		next
		break

	case OCTOPOID_ROCKETCOMEBACK
		BoxCollisionTest(C_TOUCH, object.entityPos, -80, -105, 80, 105, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
		if body.hit != 0
			body.hit--
		end if

		if body.hit == 0
			if boss.health == 90
				object.value6 = 0
				object.state++
			end if
		end if
				
		break
	case TEASE_END
		SetScreenFade(0, 0, 0, object.value6)
		object.value6 += 2
		music.volume -= 2
		if object.value6 >= 256
			stage.activeList = PRESENTATION_STAGE
			stage.listPos = 0
			LoadStage()
		endif
	break
	endswitch
	
	//not the best code, but it works fine, so....
		if object[+1].state == 4
			if object[+2].state == 4
				if object[+3].state == 4
					if object[+4].state == 4
						if object.state < OCTOPOID_ROCKETLAUNCH
							object.state = OCTOPOID_ROCKETLAUNCH
							boss.timer = 0
						endif
					endif
				endif
			endif
		endif

	
	if object.state == OCTOPOID_RISEUP
		if boss.screenShake > 0
			boss.screenShake--
			temp2 = boss.screenShake
			temp2 &= 7
			if temp2 == 0
				screen.shakeX = 1
			end if
			temp2 = boss.screenShake
			temp2 &= 3
			if temp2 == 0
				screen.shakeY = 2
			end if
		end if
	endif
	
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				temp0 = object[currentPlayer].collisionRight
				temp0 <<= 16
				temp0 += object[currentPlayer].xpos
				temp1 = stage.curXBoundary2
				temp1 <<= 16
				if temp0 > temp1
					object[currentPlayer].xvel = 0
					object[currentPlayer].speed = 0
					object[currentPlayer].xpos = temp1
					temp0 = object[currentPlayer].collisionRight
					temp0 <<= 16
					object[currentPlayer].xpos -= temp0
				end if
		next
end event

event ObjectDraw
	if boss.health <= 90
		DrawSprite(1)
	else
		DrawSprite(0)
	end if
	if body.hit != 0
		DrawSprite(5)	
	end if
	
	
	//Rocket Launcher
	


	DrawSpriteXY(boss.launcherframe, object.xpos, object.ypos)
	temp1 = object.xpos
	temp1 += 158
	DrawSpriteFX(boss.launcherframe, FX_FLIP, temp1, object.ypos)
end event

event ObjectStartup
	SetMusicTrack("BossSSZ.ogg", 4, 1)
    LoadSpriteSheet("SSZ/Objects2.gif")
	SpriteFrame(-79, -108, 158, 216, 1, 1)			//0 Body
	SpriteFrame(-79, -108, 158, 216, 1, 218)		//1 Cracked Body
	
	SpriteFrame(-81, -7, 81, 31, 160, 100)		//2 Rocket Launcher Closed
	SpriteFrame(-81, -7, 81, 31, 160, 132)		//3 Rocket Launcher Opening
	SpriteFrame(-81, -7, 81, 31, 160, 164)		//4 Rocket Launcher Open

	SpriteFrame(-42, 27, 84, 75, 160, 196)		//5 Shocked Face 1
	SpriteFrame(-42, 27, 84, 75, 160, 272)		//6 Shocked Face 2
	
	foreach (TypeName[Soul Octopoid Body], arrayPos0, ALL_ENTITIES)
		rise[arrayPos0].topYPos = object[arrayPos0].ypos
	next
		
		
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 223, 172)
end event